package me.monster.smarttraffic.ui.road;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import me.monster.smarttraffic.R;
import me.monster.smarttraffic.bo.RoadLight;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-14 11:04
 */
public class LightAdapter extends RecyclerView.Adapter<LightAdapter.Holder> {

    private List<RoadLight> roadLightList;

    LightAdapter(List<RoadLight> roadLightList) {
        this.roadLightList = roadLightList;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_road_light, parent, false);
        return new Holder(itemLayout);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        if (position != 0) {
            RoadLight light = roadLightList.get(position - 1);
            holder.tvName.setText(light.getRoadName());
            holder.tvRed.setText(String.valueOf(light.getRed()));
            holder.tvGreen.setText(String.valueOf(light.getGreen()));
            holder.tvYellow.setText(String.valueOf(light.getYellow()));
        } else {
            holder.tvName.setText(R.string.light_name);
            holder.tvRed.setText(R.string.light_red);
            holder.tvGreen.setText(R.string.light_green);
            holder.tvYellow.setText(R.string.light_yellow);
        }
    }

    @Override
    public int getItemCount() {
        return roadLightList.size() + 1;
    }

    class Holder extends RecyclerView.ViewHolder {
        private TextView tvName, tvRed, tvGreen, tvYellow;

        Holder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_light_item_name);
            tvRed = itemView.findViewById(R.id.tv_light_item_red);
            tvGreen = itemView.findViewById(R.id.tv_light_item_green);
            tvYellow = itemView.findViewById(R.id.tv_light_item_yellow);
        }
    }

}
